package com.g2room.myuall.grap.univ;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.g2room.driving.util.IpUtil;
import com.g2room.myuall.grap.univ.po.ImgPo;
import com.g2room.myuall.grap.univ.po.UnivInfoExtPo;
import com.g2room.myuall.util.DbUtil;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;

public class GrapZdxBadge implements Runnable {
	//http://www.youzy.cn/college/search?page=146
	static String pageUrl = "http://www.youzy.cn/college/search?page="; 
	static PreparedStatement  errPs = null;
	static String baseUrl = "http://www.youzy.cn";
	static String[] cats = {"校园设施","校园景观","文化生活"};
	
	public static void main(String[] args) {
//		// 以下为抓取
//		for(int i =0; i<15; i++) {
//			try {
//				Thread.sleep(100);
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
			GrapZdxBadge gue = new GrapZdxBadge();
//			Thread t = new Thread(gue);
//			t.start();
//		}
		
		Map<String, Integer> map = gue.getErrs();
		List<String> fs = gue.getFns();
		for(String url : map.keySet()) {
			if(!fs.contains(url)) {
				gue.insert2Db(map.get(url));
			}
		}
		
	}
	
	private List<String> getFns() {
		String path = "D:/userdata/myuall/badeg";
		File f = new  File(path);
		List<String> fs = new  ArrayList<String>();
		for(File file : f.listFiles()) {
			fs.add(file.getName());
		}
		return fs;
	}
	

	@Override
	public void run() {
	}
	
	public static AtomicInteger page = new AtomicInteger(1);
	private static synchronized List<String> getUrl() {
		if(page.intValue() == 147) return null;
		List<String> listUrls = new ArrayList<String>();
		for(int i = page.intValue(); i< page.intValue() + 10; i++) {
			if(i>146) break;
			listUrls.add(pageUrl+i);
		}
		page.addAndGet(10);
		return listUrls;
	}

	
	
	public void insert2Db(int id) {
		try {
			PreparedStatement univPs = DbUtil.getConn().prepareStatement("update `univ_badge`  set img = null where id =? ");
			univPs.setInt(1, id);
			univPs.execute();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	public Map<String, Integer> getErrs() {
		Map<String, Integer> errs = new HashMap<String, Integer>();
		try {
			Connection connection = DbUtil.getConn();
			Statement statement = connection.createStatement();
			String sql = "select id,img from univ_badge";
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()) {
				errs.put(rs.getString("img"), rs.getInt("id"));
			}
			rs.close();
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
		return errs;
	}
	
	

}
